﻿using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.auto
{
    public class _51Sole : Information
    {
        //登陆
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("__VIEWSTATE", FlushCode());
            pd.Add("q", "请输入产品名称！");
            pd.Add("txtUserName", name);
            pd.Add("txtPwd", pass);
            pd.Add("Button1", " 登 录 ");
            string html = source.Post("http://user.51sole.com/user/Default.aspx", pd.Read(), "http://user.51sole.com/user/Default.aspx");
            html = source.Get("http://user.51sole.com/user/WebSiteInfo.aspx?AccountID=RMMcMS", "http://user.51sole.com/user/Default.aspx");
            if (html.IndexOf("当前用户：" + name) != -1)
            {
                return true;
            }
            return false;
        }
        //发布一篇文章
        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            title = tek.Get(title);
            content = MyFunction.ToText(content);
            InformationResult result = new InformationResult();
            string html = source.Get("http://user.51sole.com/user/Commerces/CreateInforNew.aspx?BigType=1", "http://user.51sole.com/user/WebSiteInfo.aspx");
            SourceFormData sfd = new SourceFormData("utf-8");
            //头部数据
            sfd.Write("__VIEWSTATE", Regex.Match(html, "name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"([^\"]+)\"").Groups[1].Value);
            sfd.Write("q", "请输入产品名称！");
            sfd.Write("txtCommerceTitle", "供应" + title);
            sfd.Write("rbtnlsCommerceType", "2");
            sfd.Write("rbtnlsCommerceIndustry", "360");
            sfd.Write("selectIndustry1", site.TypeId[0]);
            sfd.Write("selectIndustry2", site.TypeId[1]);
            if (site.TypeId.Length > 2)
            {
                sfd.Write("selectIndustry3", site.TypeId[2]);
            }
            sfd.Write("txtKeyword1", title);
            sfd.Write("txtKeyword2", "");
            sfd.Write("txtKeyword3", "");
            sfd.Write("txtKeyword4", "");
            sfd.Write("hidOldColumnIds", "");
            sfd.Write("OldParamNames", "");
            sfd.Write("ParamNames", "");
            //发送文件
            sfd.WriteFile("UploadFile", RandomPicture.Get());
            //尾部数据
            sfd.Write("txtProductQuantity", pif.Get("库存数量"));
            sfd.Write("txtBrand", pif.Get("产品名称"));
            sfd.Write("txtCaseDetail", pif.Get("产品描述"));
            sfd.Write("txtPrice", pif.Get("价格"));
            sfd.Write("txtProductSpec", pif.Get("产品规格"));
            sfd.Write("txtTrafficDesc", pif.Get("运输方式"));
            sfd.Write("txtConsignment", pif.Get("托卖货物"));
            sfd.Write("txtCommerceContent", content);
            sfd.Write("btn_newCommerce", " 新建我的商机 ");
            sfd.Write("txtkeyname", "");
            sfd.Write("hidcid", "");
            sfd.Write("a", "1");
            //结尾
            html = source.Post("http://user.51sole.com/user/Commerces/CreateInforNew.aspx?BigType=1", sfd.GetContentType(), sfd.Read(), "http://user.51sole.com/user/Commerces/CreateInforNew.aspx?BigType=1");
            if (html.IndexOf("恭喜您，商机新建已成功") != -1 && !Regex.IsMatch(html, "51sole.com/CompanyInfoDetail_0.htm"))
            {
                result.Code = 1;
                result.Content = "http://www.51sole.com/b2b/cd_" + Regex.Match(html, "\\.51sole\\.com/CompanyInfoDetail_([0-9]+)\\.htm").Groups[1].Value + ".htm";
            }
            else if (Regex.IsMatch(html, "对不起，您今天新建商机已经达到上限（[0-9]+条）"))
            {
                result.Code = 3;
            }
            else if (html.IndexOf("alert('对不起，您还没有设置采购行业，请设置采购行业！')") != -1)
            {
                string uid = Regex.Match(html, "id=\"__VIEWSTATE\" value=\"([^\"]+)\"").Groups[1].Value;
                ParametersData pd = new ParametersData();
                pd.Add("__VIEWSTATE", "");
                pd.Add("q", "请输入产品名称！");
                pd.Add("selectTrad1", site.TypeId[0]);
                pd.Add("selectTrad2", site.TypeId[1]);
                pd.Add("hidIndutrys", Regex.Match(site.TypeId[0], "[0-9]+").Groups[0].Value + "_" + Regex.Match(site.TypeId[1], "[0-9]+").Groups[0].Value + "#");
                pd.Add("btnSubmit", "提  交");
                html = source.Post("http://user.51sole.com/User/PurchasIndustry.aspx", pd.Read(), "http://user.51sole.com/User/PurchasIndustry.aspx");
                if (html.IndexOf("提交成功") != -1)
                {
                    result.Content = "{\"error\",\"设置了采购产品成功\"}";
                }
                else
                {
                    result.Code = 4;
                    result.Content = "{\"error\",\"设置了采购产品失败\"}";
                }
            }
            else if (Regex.IsMatch(html, "<script>alert\\('[^\']+'\\)"))
            {
                result.Content = Regex.Match(html, "<script>alert\\('([^\']+)'\\)").Groups[1].Value;
            }
            return result;
        }
        //获取验证码
        private string FlushCode()
        {
            string html = source.Get("http://user.51sole.com/user/Default.aspx");
            return Regex.Match(html, "id=\"__VIEWSTATE\" value=\"([^\"]+)").Groups[1].Value;
        }
    }
}
